package me.youline.dataServer.service.front;

import me.youline.dataServer.controller.dto.UserInfoFrontDTO;
import me.youline.dataServer.controller.dto.UserRelationStatusDTO;
import me.youline.dataServer.controller.dto.UserSimpleInfoFrontDTO;
import me.youline.dataServer.entity.UserBaseInfo;
import me.youline.dataServer.entity.UserBaseInfo.From;
import me.youline.dataServer.entity.UserBaseInfo.Gender;
import me.youline.dataServer.entity.UserRelationRecorder;
import me.youline.dataServer.service.OfficialUserService;
import me.youline.dataServer.service.SocialRelationService;
import me.youline.dataServer.service.account.UserAccountService;
import me.youline.dataServer.service.support.file.FileLocatorManager;

import org.perf4j.aop.Profiled;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class UserInfoDTOAdapter {
	
	@Autowired
	private FileLocatorManager fileLocatorManager;
	
	@Autowired
	private SocialRelationService socialService;
	
	@Autowired
	private UserAccountService userAccountService;
	
	@Autowired
	private OfficialUserService officialService;
	
	/**
	 * 根据用户基本信息领域对象获取用户基本信息DTO对象
	 * @param u
	 * @return
	 */
	@Profiled(tag="front-获取用户前端展示DTO信息")
	public UserInfoFrontDTO getUserBaseInfoDTO(UserBaseInfo u){
		UserInfoFrontDTO dto = new UserInfoFrontDTO();
		dto.setAvatarUrl(fileLocatorManager.getAvatarFileLocator().getFileWebUrl(u.getAvatarUrl()));
		dto.setBirthday(u.getBirthday());
		dto.setNickName(u.getNickName());
		dto.setGender(u.getGender().ordinal());
		dto.setUserId(u.getUserId());
		dto.setPhoneNum(u.getPhoneNum());
		dto.setUserName(u.getPhoneNum());
		dto.setIsOfficial(officialService.isOfficalUser(u.getUserId()));
		return dto;
	}
	
	@Profiled(tag="front-获取用户前端展示信息(包含用户社交关系)")
	public UserInfoFrontDTO getUserDTOContainsSocialRel(UserBaseInfo u,Long viewUserId){
		UserInfoFrontDTO userDTO = this.getUserBaseInfoDTO(u);
		UserRelationStatusDTO statusDTO = new UserRelationStatusDTO();
		statusDTO.setCurrentUserId(u.getUserId());
		statusDTO.setTargetUserId(viewUserId);
		if(u.getUserId().equals(viewUserId)){//当请求用户与用户信息所属用户为同一人则不需要查询关系数据
			statusDTO.setIsFollowing(false);
			statusDTO.setIsFollowed(false);
		}else{
			statusDTO.setIsFollowing(socialService.isFollowing(u.getUserId(), viewUserId));
			statusDTO.setIsFollowed(socialService.isFollowed(u.getUserId(), viewUserId));
		}
		UserRelationRecorder r = socialService.getSocialTotalRecorder(u.getUserId());
		userDTO.setFollowerNum(r == null ? 0 : r.getFollowersTotal());
		userDTO.setFollowingNum(r == null ? 0 : r.getFollowingTotal());
		userDTO.setRelStatus(statusDTO);
		userDTO.setIsOfficial(officialService.isOfficalUser(u.getUserId()));
		return userDTO;
	}
	
	/**
	 * 根据用户基本信息DTO获取用户基本信息领域对象
	 * @param dto
	 * @return
	 */
	public UserBaseInfo getUserBaseInfo(UserInfoFrontDTO dto){
		UserBaseInfo baseInfo = new UserBaseInfo();
		baseInfo.setCreatedFrom(From.APP_REGISTER);
		baseInfo.setGender(dto.getGender() == null ? null : Gender.values()[dto.getGender()]);
		baseInfo.setNickName(dto.getNickName());
		baseInfo.setBirthday(dto.getBirthday());
		return baseInfo;
	}
	
	
	/**
	 * 根据用户id获取用户简略信息的DTO
	 * @param userId
	 * @return
	 */
	@Profiled(tag="front-获取用户前端展示简略DTO信息")
	public UserSimpleInfoFrontDTO getSimpleInfoDTO(Long userId) {
			UserBaseInfo u = userAccountService.getUserBaseInfo(userId);
			UserSimpleInfoFrontDTO dto = new UserSimpleInfoFrontDTO();
			dto.setUserId(userId);
			dto.setGender(u.getGender().ordinal());
			dto.setNickName(u.getNickName());
			dto.setAvatarUrl(fileLocatorManager.getAvatarFileLocator().getFileWebUrl(u.getAvatarUrl()));
			dto.setIsOfficial(officialService.isOfficalUser(userId));
			return dto;
	}

}
